#include "CMainMenuState.h"
#include "CGame.h"
#include "COptionMenuState.h"
#include "CGamePlayState.h"
#include "CXInput.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_DirectSound.h"
#include "SGD Wrappers/CSGD_WaveManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "CConfigAudioState.h"
#include "SGD Wrappers/CSGD_FModManager.h"
#include "CConfigControlState.h"
#include "CConfigVideoState.h"
#include "CExitState.h"
#include "Mail System.h"
#include "CUnit.h"
#include "CObjectManager.h"
#include "CObjGenerator.h"
#include "CEventSystem.h"

CConfigVideoState::CConfigVideoState()
{
	Type(SUB);
	m_bfItem.Init("Ludicrum_Reclamation_Alt.fnt");
	m_nBGImage = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/piedpiper2.jpg");

}
CConfigVideoState::~CConfigVideoState()
{

}
void CConfigVideoState::Enter()
{
	/////////////////////////////////////////////////
	//	Menu Items
	CMenuItem miPlay;
	miPlay.Init(&m_bfItem, 200, 0, "Full Screen", CConfigVideoState::GetInstance(), 1.0f);
	miPlay.GoTo(200, 300);
	m_vMenuItems.push_back(miPlay);
	//CMenuItem miOptions;
	//miOptions.Init(&m_bfItem, 200, 0, "", CConfigAudioState::GetInstance(), 1.0f);
	//miOptions.GoTo(400, 300);
	//m_vMenuItems.push_back(miOptions);
	/*CMenuItem miKer;
	miKer.Init(&m_bfItem, 200, 0, "Controls", CConfigControlState::GetInstance(), 1.0f);
	miKer.GoTo(600, 300);
	m_vMenuItems.push_back(miKer);*/
	CMenuItem miExit;
	miExit.Init(&m_bfItem, 200, 0, "Back", CExitState::GetInstance(), 1.0f);
	miExit.GoTo(600, 300);
	m_vMenuItems.push_back(miExit);
	//
	/////////////////////////////////////////////////

	/*ObjectGenerator<string, CBase>::GetInstance()->RegisterClassType<CUnit>("CUnit");
	MailSystem::GetInstance()->InitMessageSystem(CGame::GetInstance()->MessageProc);

	for(int i = 0; i < 100; ++i)
		MailSystem::GetInstance()->SendMail(new SpawnUnitMenu());


	MailSystem::GetInstance()->ReadMail();*/
}
void CConfigVideoState::Exit()
{
	m_vMenuItems.clear();

	/*MailSystem::GetInstance()->ShutdownMailSystem();
	ObjectGenerator<string, CBase>::GetInstance()->UnregisterClassType("CUnit");
	ObjectGenerator<string, CBase>::GetInstance()->ShutdownObjectFactory();*/
	//CObjectManager::GetInstance()->Clear();
}
bool CConfigVideoState::Input()
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_ESCAPE))
	{
		CGame::GetInstance()->PopState();
	}

	if(	CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_DOWN) || 
		CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RIGHT) ||
		((CXInput::GetInstance()->LStickY() <= -7850 ||
		CXInput::GetInstance()->LStickX() >= 7850) && CXInput::GetInstance()->IsLStickMoved() == false))
	{

		//////////////////////////////////////////////////////////
		//	BUG FIX
		//	Reference Bug # BB-006
		//	Approved by : ?
		//	BUG FIX START
		//////////////////////////////////////////////////////////
		CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChoiceSound());
		CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChoiceSound(), CGame::GetInstance()->SFXVol());
		CXInput::GetInstance()->IsLStickMoved(true);
		vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();
		for(unsigned int i = 0; i < m_vMenuItems.size() - 1; i++)
			StuffIter++;
		float fTempX = StuffIter->TarPosX();
		float fTempY = StuffIter->TarPosY();

		while(StuffIter != m_vMenuItems.begin())
		{
			vector<CMenuItem>::iterator temp = StuffIter;

			--StuffIter;

			temp->GoTo(StuffIter->TarPosX(), StuffIter->TarPosY());

			if(StuffIter->Target() == m_vMenuItems[0].Target())
			{
				StuffIter->GoTo(fTempX, fTempY);
				break;
			}
		}
		//////////////////////////////////////////////////////////
		//	BUG FIX END Reference # BB-006
		//////////////////////////////////////////////////////////
	}

	if(	CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_UP) || 
		CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_LEFT) ||
		((CXInput::GetInstance()->LStickY() >= 7850 ||
		CXInput::GetInstance()->LStickX() <= -7850) && CXInput::GetInstance()->IsLStickMoved() == false))
	{
		CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChoiceSound());
		CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChoiceSound(), CGame::GetInstance()->SFXVol());
		CXInput::GetInstance()->IsLStickMoved(true);
		vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();
		float fTempX = StuffIter->TarPosX();
		float fTempY = StuffIter->TarPosY();

		while(StuffIter != m_vMenuItems.end())
		{
			vector<CMenuItem>::iterator temp = StuffIter;

			StuffIter++;

			temp->GoTo(StuffIter->TarPosX(), StuffIter->TarPosY());

			if(StuffIter->Name() == m_vMenuItems[m_vMenuItems.size()-1].Name())
			{
				StuffIter->GoTo(fTempX, fTempY);
				break;
			}
		}
	}
	if((CXInput::GetInstance()->LStickY() > -7850 && CXInput::GetInstance()->LStickY() < 7850 &&
		CXInput::GetInstance()->LStickX() < 7850 &&	CXInput::GetInstance()->LStickX() > -7850)
		&& CXInput::GetInstance()->IsLStickMoved() == true)
	{
		CXInput::GetInstance()->IsLStickMoved(false);
	}

	if(	((CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RETURN)) && (CSGD_DirectInput::GetInstance()->KeyUp(DIK_LALT))) ||
		CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A && CXInput::GetInstance()->GetIsADown() == false)
	{
		CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChosenSound());
		CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChosenSound(), CGame::GetInstance()->SFXVol());
		CXInput::GetInstance()->SetIsADown(true);
		vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

		while(StuffIter != m_vMenuItems.end())
		{
			if(StuffIter->TarPosX() == 200)
			{
				if(StuffIter->Name() == "Back")
				{
					CGame::GetInstance()->PopState();
					return true;
				}
				if(StuffIter->Name() == "Full Screen")
				{
					CGame::GetInstance()->IsWindowed(!(CGame::GetInstance()->IsWindowed()));
					return true;
				}
			}
			++StuffIter;
		}
	}
	else if(!(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A) && CXInput::GetInstance()->GetIsADown() == true)
		CXInput::GetInstance()->SetIsADown(false);
	return true;
}
void CConfigVideoState::Update(float fDelta)
{
	vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

	while(StuffIter != m_vMenuItems.end())
	{
		StuffIter->Update(fDelta);
		++StuffIter;
	}


	/*MailSystem::GetInstance()->ReadMail();
	CEventSystem::GetInstance()->ProcessEvents();
	CObjectManager::GetInstance()->Update(fDelta);*/
}
void CConfigVideoState::Render()
{
	/////////////////////////////////////////////////////////////////
	// BUG FIX
	// Reference Bug # BB-005
	// Approved by: 
	// BUG FIX START
	/////////////////////////////////////////////////////////////////
	CSGD_TextureManager::GetInstance()->Draw(m_nBGImage,0, 0, .8f, .6f);
	///////////////////////////////////////////////////////////////////////////
	// BUG FIX END  Reference # BB-005
	//////////////////////////////////////////////////////////////////////////

	//CObjectManager::GetInstance()->Render();

	m_bfItem.DrawBitmapText("Video_Config_Menu", 25, 100, 2.0f);

	vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

	while(StuffIter != m_vMenuItems.end())
	{
		if(StuffIter->TarPosX() == 200)
			StuffIter->Render(true);
		else
			StuffIter->Render();

		CSGD_Direct3D::GetInstance()->SpriteEnd();
		char buffer[128];
		if(StuffIter->TarPosX() == 200)
		{
			if(StuffIter->Name() == "Full Screen")
			{
				if(CGame::GetInstance()->IsWindowed() == true)
				{
					sprintf_s(buffer, _countof(buffer), "Full Screen");
				}
				else
				{
					sprintf_s(buffer, _countof(buffer), "Windowed");
				}
				CSGD_Direct3D::GetInstance()->DrawText(buffer, (int)StuffIter->PosX(), (int)StuffIter->PosY() + 50);
			}
		}
		CSGD_Direct3D::GetInstance()->SpriteBegin();

		++StuffIter;
	}
}